import java.util.ArrayList;
import java.util.List;

public class Solution {
    public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }
    public List<String> ans;
    public List<String> binaryTreePaths(TreeNode root) {
        ans=new ArrayList<>();
        dfs(root,new StringBuffer());
        return ans;
    }
    public void dfs(TreeNode root,StringBuffer path){
        StringBuffer tem=new StringBuffer(path);
        if(root.right==null && root.left==null){
            ans.add(tem.toString());
            return ;
        }
        tem.append(Integer.toString(root.val));
        tem.append("->");
        if(root.left!=null){
            dfs(root.left,tem);
        }
        if(root.right!=null){
            dfs(root.right,tem);
        }
    }
}
